package com.detrum.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.detrum.bean.VersionBean;
import com.detrum.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by abc on 2018/3/9.
 */

public class SqliteVersionOperate {
    private static SqliteVersionOperate operate;
    private SQLiteDatabase sqLiteWrite, sqLiteRead;
    private final String tableName = "Device_Version";
    private final String DeviceName = "name";                                         //设备名称
    private final String DeviceVersion = "deviceVersion";                             //最近连接设备版本--从设备上读取到的
    private final String DeviceDownLoadVersion = "version";                           //设备网络版本
    private final String DeviceFileLength = "length";                                 //设备网络版本文件长度
    private final String DeviceVerify = "verify";                                     //设备网络版本校验码
    private final String DeviceDownLoadPath = "downloadPath";                         //设备网络版本下载地址
    private final String DeviceFilePath = "filePath";                                 //设备本地安装文件路径
    private final String DeviceDownLoadImagePath = "imagePath";                       //设备广告图片文件下载路径
    private final String DeviceExplain = "explain";                                   //设备文件说明--文字信息
    private final String DeviceUpdateMsg = "updateMsg";                               //设备更新说明
    private final String DeviceDownLoadUpdateFilePath = "updateFilePath";             //设备更新说明文件路径---广告或详情文件
    private final String DeviceUpdateTime = "updateTime";                             //设备更新时间

    private SqliteVersionOperate(Context context) {
        sqLiteWrite = SqliteHelper.getInstance(context).getWritableDatabase();
        sqLiteRead = SqliteHelper.getInstance(context).getReadableDatabase();
    }

    public static SqliteVersionOperate getInstance(Context context) {
        if (operate == null) {
            operate = new SqliteVersionOperate(context);
        }
        synchronized (operate) {
            return operate;
        }
    }

    public int Insert(VersionBean bean){
        if(bean==null){
            return -1;
        }
        String fileName = bean.getName();
        LogUtils.e("fileName = "+fileName);
        if(fileName==null||fileName.length()==0){
            return -5;
        }
        int select = SelectSingle(fileName);
        LogUtils.e("select = "+select);
        if(select!=-1){
            int update = Updata(bean);
            LogUtils.e("update = "+update);
            return update;
        }
        ContentValues values = new ContentValues();
        values.put(DeviceName,fileName);
        if(bean.getDeviceVersion()==null||bean.getDeviceVersion().length()==0){
            return -6;
        }
        values.put(DeviceVersion,bean.getDeviceVersion());
        values.put(DeviceDownLoadVersion,bean.getNetVersion());
        values.put(DeviceFileLength,bean.getLength());
        values.put(DeviceVerify,bean.getVerify());
        values.put(DeviceDownLoadPath,bean.getFilePath());
        values.put(DeviceFilePath,bean.getLocalFilePath());
        values.put(DeviceDownLoadImagePath,bean.getImagePath());
        values.put(DeviceExplain,bean.getExplain());
        values.put(DeviceUpdateMsg,bean.getUpdataMsg());
        values.put(DeviceDownLoadUpdateFilePath,bean.getUpdataFilePath());
        values.put(DeviceUpdateTime,bean.getUpdateTime());
        long insert = sqLiteWrite.insert(tableName,null,values);
        LogUtils.i("insert = "+insert);
        return (int) insert;
    }

    public int Delete(String fileName){
        if(fileName==null){
            return -1;
        }
        sqLiteWrite.delete(tableName,DeviceName+" = ?",new String[]{fileName});
        return 0;
    }

    public int Updata(VersionBean bean){
        if(bean==null){
            return -1;
        }
        String fileName = bean.getName();
        ContentValues values = new ContentValues();
        if(bean.getLocalFilePath()!=null){
            values.put(DeviceFilePath,bean.getLocalFilePath());
        }
        if(bean.getFilePath()!=null){
            values.put(DeviceDownLoadPath,bean.getFilePath());
        }
        if(bean.getDeviceVersion()!=null){
            values.put(DeviceVersion,bean.getDeviceVersion());
        }
        if(bean.getNetVersion()!=null){
            values.put(DeviceDownLoadVersion,bean.getNetVersion());
        }
        if(bean.getUpdateTime()!=0){
            values.put(DeviceUpdateTime,bean.getUpdateTime());
        }
        if(bean.getLength()!=0){
            values.put(DeviceFileLength,bean.getLength());
        }
        if(bean.getVerify()!=null){
            values.put(DeviceVerify,bean.getVerify());
        }
        if(bean.getImagePath()!=null){
            values.put(DeviceDownLoadImagePath,bean.getImagePath());
        }
        if(bean.getExplain()!=null){
            values.put(DeviceExplain,bean.getExplain());
        }
        if(bean.getUpdataMsg()!=null){
            values.put(DeviceUpdateMsg,bean.getUpdataMsg());
        }
        if(bean.getUpdataFilePath()!=null){
            values.put(DeviceDownLoadUpdateFilePath,bean.getUpdataFilePath());
        }
        int update = sqLiteWrite.update(tableName,values,DeviceName+" = ?",new String[]{fileName});
        return update;
    }

    public List<VersionBean> SelectAll(){
        List<VersionBean>list = new ArrayList<>();
        Cursor cursor = sqLiteRead.query(tableName,null,null,null,null,null,null);
        while (cursor.moveToNext()){
            String deviceName = cursor.getString(cursor.getColumnIndex(DeviceName));
            String deviceVerify = cursor.getString(cursor.getColumnIndex(DeviceVerify));
            String deviceVersion = cursor.getString(cursor.getColumnIndex(DeviceVersion));
            String deviceNetVersion = cursor.getString(cursor.getColumnIndex(DeviceDownLoadVersion));
            String deviceDownloadPath = cursor.getString(cursor.getColumnIndex(DeviceDownLoadPath));
            String deviceUpdateMsg = cursor.getString(cursor.getColumnIndex(DeviceUpdateMsg));
            String deviceExplain = cursor.getString(cursor.getColumnIndex(DeviceExplain));
            int deviceFileLength = cursor.getInt(cursor.getColumnIndex(DeviceFileLength));
            String devieImagePath = cursor.getString(cursor.getColumnIndex(DeviceDownLoadImagePath));
            String deviceUpdataPath = cursor.getString(cursor.getColumnIndex(DeviceDownLoadUpdateFilePath));
            VersionBean bean = new VersionBean();
            bean.setName(deviceName);
            bean.setDeviceVersion(deviceVersion);
            bean.setNetVersion(deviceNetVersion);
            bean.setVerify(deviceVerify);
            bean.setFilePath(deviceDownloadPath);
            bean.setUpdataMsg(deviceUpdateMsg);
            bean.setLength(deviceFileLength);
            bean.setExplain(deviceExplain);
            bean.setImagePath(devieImagePath);
            bean.setUpdataFilePath(deviceUpdataPath);
            list.add(bean);
        }
        return list;
    }

    public VersionBean Select(String name){
        VersionBean bean=null;
        String[]values = {name};
        Cursor cursor = sqLiteRead.query(tableName,null,DeviceName+" = ?",values,null,null,null);
        if(cursor.moveToFirst()){
            String deviceName = cursor.getString(cursor.getColumnIndex(DeviceName));
            String netVersion = cursor.getString(cursor.getColumnIndex(DeviceDownLoadVersion));
            int length = cursor.getInt(cursor.getColumnIndex(DeviceFileLength));
            String verify = cursor.getString(cursor.getColumnIndex(DeviceVerify));
            String filePath = cursor.getString(cursor.getColumnIndex(DeviceDownLoadPath));
            String localFilePath = cursor.getString(cursor.getColumnIndex(DeviceFilePath));
            long updateTime = cursor.getLong(cursor.getColumnIndex(DeviceUpdateTime));
            String imagePath = cursor.getString(cursor.getColumnIndex(DeviceDownLoadImagePath));
            String explain = cursor.getString(cursor.getColumnIndex(DeviceExplain));
            String updateMsg = cursor.getString(cursor.getColumnIndex(DeviceUpdateMsg));
            String updateFilePath = cursor.getString(cursor.getColumnIndex(DeviceDownLoadUpdateFilePath));
            bean = new VersionBean(deviceName,netVersion,length,verify,filePath,localFilePath,imagePath,explain,updateMsg,updateFilePath,updateTime);
        }
        return bean;
    }

    public int SelectSingle(String name){
        String[]values = {name};
        Cursor cursor = sqLiteRead.query(tableName,null,DeviceName+" = ?",values,null,null,null);
        int count = cursor.getCount();
        if(count>0){
            return count;
        }else{
            return -1;
        }
    }
}
